package postpc.powerreminders.com.systemEvents;

import postpc.powerreminders.com.db.RotgDatabase;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class OutgoingCallReceiver extends BroadcastReceiver {

	/*
	 * Logger tag
	 */
	private static final String TAG = "ROTG_SYS_EVENTS";

	private static final int PHONE_REMINDER_TIME = 10000;
	
	/*
	 * Reference to the data base.
	 */	
	static RotgDatabase dataBase;

	
	public OutgoingCallReceiver(){
		super();
	}
	 /**
	  * 
	  * @param db
	  */
	public OutgoingCallReceiver(RotgDatabase db) {
		super();
		OutgoingCallReceiver.dataBase = db;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();

		if(null == bundle)
			return;

		String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

		Log.i(TAG, "Outgoing call to:" + phonenumber);

		Cursor cursor = OutgoingCallReceiver.dataBase.checkCallEvent(phonenumber);
		
		// ALERT
		if (0 != cursor.getCount()){
			Toast notification = Toast.makeText(context, cursor.getString(2), Toast.LENGTH_LONG);
			notification.setDuration(PHONE_REMINDER_TIME);
			notification.show();
			dataBase.deactivateReminder(cursor.getInt(0));
		}
	}
}